import Vue from "vue";
// import Vuex from "vuex";
import Vuex from "../vuex/index";

Vue.use(Vuex);

// 持久化state
const persists = (store, key = "VUEX", storageName = "sessionStorage") => {
  const storage = window[storageName];

  let state = storage.getItem(key);

  if (state) {
    store.replaceState(JSON.parse(state));
  }

  store.subscribe((mutationType, rootState) => {
    storage.setItem(key, JSON.stringify(rootState));
  });
}

const store = new Vuex.Store({
  strict: true,
  plugins: [
    // (store) => persists(store, "VUE_PERSISTS", "localStorage")
    persists
  ],
  state: {
    age: 10
  },
  getters: {
    myage(state) {
      console.log("runer")
      return state.age + 10
    }
  },
  mutations: {
    add(state, payload) {
      console.log("根模块", state)
      state.age += payload
    }
  },
  actions: {
    add({ commit }, payload) {
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          commit("add", payload)

          resolve()
        }, 1000)
      })
    }
  },
  modules: {
    a: {
      namespaced: true,
      state: {
        age: 100,
        a: "模块a"
      },
      getters: {
        myage(state) {
          return state.age + 100
        }
      },
      mutations: {
        add(state, payload) {
          console.log("a模块", state)
          state.age += payload
        },
        cbb(state, payload) {
          state.age += payload
        },
      },
      actions: {
        add({ commit }, payload) {
          setTimeout(() => {
            commit("a/add", payload)
          }, 1000)
        }
      },
      modules: {
        aa: {
          namespaced: true,
          state: {
            age: 1000,
            aa: "a模块的aa"
          },
          getters: {
            myage(state) {
              return state.age + 100
            }
          },
          mutations: {
            add(state, payload) {
              console.log("aa模块", state)
              state.age += payload
            },
            cbb(state, payload) {
              state.age += payload
            },
          },
          actions: {
            add({ commit }, payload) {
              setTimeout(() => {
                commit("a/aa/add", payload)
              }, 1000)
            }
          },
        }
      }
    },
    b: {
      namespaced: true,
      state: {
        age: 10000,
        b: "模块b"
      },
      mutations: {
        add(state, payload) {
          console.log("b模块", state)
          state.age += payload
        }
      },
      actions: {
        add({ commit }, payload) {
          setTimeout(() => {
            commit("b/add", payload)
          }, 1000)
        }
      },
    }
  }
})

// 动态注册路由
store.registerModule(["a", "c"], {
  namespaced: true,
  state: {
    name: "c",
    b: "模块c"
  },
  getters: {
    myName(state) {
      return state.name + "c"
    }
  },
  mutations: {
    add(state, payload) {
      console.log("c模块")
      state.name += payload
    }
  },
})

export default store
